//
//  CollectionViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "CollectionViewController.h"
#import "CollectionCell.h"
#import "LSCollectionDataBase.h"
#import "SubjectModel.h"
#import "LSSubjDetailViewController.h"
#import <Masonry.h>
@interface CollectionViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *indexA;
@property (nonatomic, retain)UILabel *titleLabel;

@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = self.titleLabel;
    self.titleLabel.text = @"我的收藏";
    [self.titleLabel sizeToFit];
    
    [[LSCollectionDataBase shareDataBase] openDB];
    [[LSCollectionDataBase shareDataBase] createTable];
    self.array = [NSMutableArray array];
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.array = [[LSCollectionDataBase shareDataBase] select];
    self.myTableView.tableFooterView = [UIView new];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.backgroundColor = [UIColor clearColor];
    [self.myTableView registerClass:[CollectionCell class] forCellReuseIdentifier:@"LSCollectCell"];
    
    [self noImage];
    
}
- (void)noImage{
    if (self.array.count == 0) {
        UIImageView *bgimg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-biaoqingwunai2.png"]];
        [self.view addSubview:bgimg];
        [bgimg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(100);
            make.right.mas_equalTo(-100);
            make.top.mas_equalTo(100);
            make.height.equalTo(self.view.mas_height).multipliedBy(0.3);
        }];
        
    }
}
- (void)leftAction{
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"rippleEffect";
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"LSCollectCell";
    CollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.backgroundColor = [UIColor clearColor];
    SubjTVModel *subM = [self.array objectAtIndex:indexPath.row];
//    NSLog(@"car ===+++++== %ld", subM.experience_id);
    cell.subTVM = subM;
    return cell;
}
#pragma mark -知识点  询问哪行需要编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    //返回值为yes,每行都要编辑
    return YES;
}

#pragma mark -知识点  设置哪行的编辑状态(删除/添加)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //每行都是删除状态
    return UITableViewCellEditingStyleDelete;
}

#pragma mark -知识点  实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SubjTVModel *subM = [self.array objectAtIndex:indexPath.row];
    
    NSString *name = subM.experience_title;
    NSLog(@"************name = %@", name);
    
    //根据电影名删除数据库里的数据
    [[LSCollectionDataBase shareDataBase]delete:name];
    //删除
    NSLog(@"indexPath-----%@", indexPath);
    [self.array removeObjectAtIndex:indexPath.row];
    //删除cell
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
    [self noImage];
    [self.myTableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150 * HEIGHT / 667;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CATransition *transition = [CATransition animation];
    transition.duration = 0.7f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"cube";
    transition.subtype = kCATransitionFromRight;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    LSSubjDetailViewController *lsSubDetail = [[LSSubjDetailViewController alloc] init];
    lsSubDetail.subTVM = [_array objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:lsSubDetail animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
